package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.mvp.model.Goods1Model;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/10/26
 */
public class PublicGoods1Presenter implements PublicGoods1Model {

    private final String TAG = getClass().getSimpleName();
    private PublicGoods1View goods1View;
    private Context mContext;

    public PublicGoods1Presenter(PublicGoods1View goods1View, Context mContext) {
        this.goods1View = goods1View;
        this.mContext = mContext;
    }

    @Override
    public void uploadGoods(String warehouse_id, String path, String no, String goods_name, String country_id, String class_id, String sold_least_num, String market_price, String is_free_post, String use_ydx, String free_post_num, String json, String num, String specifications, String packge, String weight, String volume, String single_num, String single_specifications) {
        HttpParams params = new HttpParams();
        params.put("warehouse_id", warehouse_id);
        params.put("path", path);
        params.put("no", no);
        params.put("goods_name", goods_name);
        params.put("country_id", country_id);
        params.put("class_id", class_id);
        params.put("sold_least_num", sold_least_num);
        params.put("market_price", market_price);
        params.put("is_free_post", is_free_post);
        if (!Tools.isNull(use_ydx)) {
            params.put("use_ydx", use_ydx);
        }
        params.put("free_post_num", free_post_num);
        params.put("json", json);
        params.put("num", num);
        params.put("specifications", specifications);
        params.put("package", packge);
        params.put("weight", weight);
        params.put("volume", volume);
        params.put("single_num", single_num);
        if (Tools.isNull(single_specifications)) {
            single_specifications = "";
            params.put("single_specifications", single_specifications);
        } else {
            params.put("single_specifications", single_specifications);
        }
        OkHttpUtils.post(HttpUtil.PUBLIC_GOODS1)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, Goods1Model.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        goods1View.publicGoods1Success((Goods1Model) data);
                        goods1View.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        goods1View.hideLoading();
                        goods1View.httpStatusError();
                    }
                });
    }
}
